<?php
//
// 分享家提现
//
class Controller_Admin_SharePayment extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'sharepayment';
        $this->_no_buttons = TRUE;
        $this->_list_pagesize = 20;
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

    public function action_handle ($id)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $status   = Arr::get($post, 'status');
            $shareUid = Arr::get($post, 'shareUid');
            $user     = User::getUserFromUid($shareUid);
            $shareUser = new App_Share_User($user);
            $shareUser->setCashMoneyStatus($id, $status);
            $this->request->redirect('/admin/sharepayment/list');
        }
        else
        {
           $detail = App_Share_Money::paymentDetail($id); 
           $this->template->set_filename('smarty:admin/share/handle_payment');
           $this->template->set('detail', $detail);
        }
    }

	//添加分享家
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改分享家
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['money']       = number_format($this->_form_data['money']/100, 2);
        }

        parent::action_update($nid);
    }

	// 详情
	//$nid int 商铺ID
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['money']       = number_format($this->_form_data['money']/100, 2);
        $this->_form_data['ltMoneyFee']  = number_format($this->_form_data['ltMoneyFee']/100, 2);
        $this->_form_data['egtMoneyFee'] = number_format($this->_form_data['egtMoneyFee']/100, 2);

        parent::action_detail($nid);
    }

	// 删除分享家
    public function action_delete ($nid)
    {
        App_Deliver::remove($nid);
        $this->to_url('list');
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        //
        $post['ltMoneyFee']  = (int) ($post['ltMoneyFee'] * 100);
        $post['egtMoneyFee'] = (int) ($post['egtMoneyFee'] * 100);
        $post['money']       = (int) ($post['money'] * 100);
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $rs['money']  = '￥'.number_format($rs['money']/100, 2);
            $rs['statusId'] = $rs['status'];
            $rs['status'] = App_Share_Money::paymentStatusName($rs['status']);
            $rs['rawShareUid'] = $rs['shareUid'];
            $rs['shareUid'] = App_Share_User::shareUserName($rs['shareUid']);
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
        array_pop($menus);
        array_pop($menus);
        array_pop($menus);

        $menus[] = '<a href="/admin/shareuser/income/'.$row['rawShareUid'].'">收入流水</a>';
        if ($row['statusId'] < 2)
            $menus[] = '<a href="/admin/sharepayment/handle/'.$row['id'].'">处理提现</a>';
    }
}
